package com.sk.cardeal.dialog;

import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.KeyBoardAdapter;
import com.sk.cardeal.callback.MyCallBack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by sk on 2018/3/15.
 */

public class KeyBoardDialogFtagment extends DialogFragment implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    Unbinder unbinder;
    private MyCallBack myCallBack;

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    private String[] strings = new String[]{"浙","闽","粤","京","津","冀","晋","蒙","辽","吉","黑","沪","苏","皖","赣","鲁","豫","鄂","湘","桂","琼","渝","川","贵","云","藏","陕","甘","青","宁","新"};


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_keyboard, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        dataRyc.setLayoutManager(new GridLayoutManager(getActivity(),10));
        List<String> stringList = new ArrayList<>();
        stringList.addAll(Arrays.asList(strings));
        KeyBoardAdapter adapter = new KeyBoardAdapter(R.layout.item_board,stringList);
        dataRyc.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }

    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.0f;
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(layoutParams);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        dismiss();
        if(myCallBack!=null){
            myCallBack.selectArea(strings[position]);
        }
    }
}
